package com.atguigu.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.atguigu.pojo.Book;
import com.atguigu.pojo.Page;
import com.atguigu.service.BookService;
import com.atguigu.service.impl.BookServiceImpl;
import com.atguigu.utils.Utils;

/**
 * Servlet implementation class BookServlet
 */
public class BookServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
    private BookService bookService = new BookServiceImpl();
    
	protected void saveBook(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Book book = Utils.copyParamToBean(request.getParameterMap(), new Book());
		//System.out.println(request.getMethod());
		bookService.saveBook(book);
		response.sendRedirect(request.getContextPath() + 
				"/manager/bookServlet?action=getPage&pageNo="+request.getParameter("pageNo"));
	}
	
	protected void updateBook(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Book book = Utils.copyParamToBean(request.getParameterMap(), new Book());
		bookService.updateBook(book);
	
		response.sendRedirect(request.getContextPath() + 
				"/manager/bookServlet?action=getPage&pageNo="+request.getParameter("pageNo"));
	}
	
	protected void getOneBook(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int parseInt = Utils.parseInt(request.getParameter("id"), 0);
		Book book = bookService.queryOne(parseInt);
		request.setAttribute("book", book);
		request.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(request, response);
	}
	
	protected void deleteBook(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id = request.getParameter("id");
		int parseInt = Utils.parseInt(id, 0);
		bookService.deleteBook(parseInt);
		response.sendRedirect(request.getContextPath() +
				"/manager/bookServlet?action=getPage&pageNo="+request.getParameter("pageNo"));
		
	}
	
	protected void getAllBook(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<Book> list = bookService.queryAllBook();
		request.setAttribute("listBooks", list);
		request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request, response);
	}

	protected void getPage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int pageNo = Utils.parseInt(request.getParameter("pageNo"), 1);
		int pageSize = Utils.parseInt(request.getParameter("pageSize"), Page.PAGE_SIZE);
		
		Page<Book> page = bookService.page(pageNo,pageSize);
		
		page.setUrl("manager/bookServlet?action=getPage");
		request.setAttribute("page", page);
		request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request, response);
		
	}

}
